package com.cw.lang.common.mapper.provider.list;

import org.apache.ibatis.mapping.MappedStatement;

import com.cw.lang.common.exception.MybatisException;
import com.cw.lang.common.enums.EnumDialect;
import com.cw.lang.common.enums.EnumMybatisError;
import com.cw.lang.common.mapper.utils.DialectUtils;

import tk.mybatis.mapper.additional.dialect.oracle.OracleProvider;
import tk.mybatis.mapper.additional.insert.InsertListProvider;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;

/**
 * @author chenw
 * @version V2.1
 * @since 2.1.0 2019-04-09 17:08
 */
public class InsertListProviderWrapper extends MapperTemplate {

    public InsertListProviderWrapper(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * 批量插入
     *
     * @param ms MappedStatement
     * @return sql语句
     */
    public String insertList(MappedStatement ms) {
        // 获取方言
        EnumDialect dialect = DialectUtils.getDialect(ms.getConfiguration()
            .getEnvironment()
            .getDataSource());
        // 暂时使用switch处理，会因为现在仅有2种方言，如果后续需要添加，则要考虑换成策略模式
        switch (dialect) {
            case MYSQL:
                return new InsertListProvider(mapperClass, mapperHelper).insertList(ms);
            case ORACLE:
                return new OracleProvider(mapperClass, mapperHelper).insertList(ms);
            default:
                throw new MybatisException(EnumMybatisError.DIALECT_NOT_EXISTS.getCode(),
                    EnumMybatisError.DIALECT_NOT_EXISTS.getDescription());
        }
    }

}
